<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 订单游客信息表模型类
 * Created by PhpStorm.
 * User: 66diqiu-li
 * Date: 14-8-1
 * Time: 上午11:09
 */
class Order_Ordertourist_model extends CI_Model{

    /**
     * 更新数据
     * @param array $data  数组
     * @param mixed $where 查询条件 array|string
     * @return boolean 返回数据 true/false
     */
    public function edit_row($data,$where){
        $table = $this->_get_table_name();
        if($data['id_number']&&strlen($data['id_number'])==18){
            $data['birth'] =  @sub_str($data['id_number'],6,4);
        }
        if(!empty($where)) $this->db->where($where, NULL, FALSE);
        return $this->db->update($table,$data);
    }

    /**
     * 批量添加游客信息
     * @param  array $data
     * @return bool
     */
    public function add_batch($data){
        $table = $this->_get_table_name();
        if(is_array($data)&&!empty($data)){
            foreach($data as $key=>$val){
                if(!isset($val['sid'])){
                    $data[$key]['sid'] = $GLOBALS['sid'];
                    if($val['id_number']&&strlen($val['id_number'])==18){
                        $data[$key]['birth'] =  @sub_str($val['id_number'],6,4);
                    }
                }
            }


            if($this->db->insert_batch($table,$data)){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }

    /**
     * 添加数据
     * @param array $data 数组
     * @return mixed 返回数据 false/[>0]
     */
    public function add_row($data){
        $table = $this->_get_table_name();
        if(is_array($data)){
            if(!isset($data['sid'])){
                $data['sid'] = $GLOBALS['sid'];
            }
            if($data['id_number']&&strlen($data['id_number'])==18){
                $data['birth'] =  @sub_str($data['id_number'],6,4);
            }

            if($this->db->insert($table,$data)){
                return $this->db->insert_id();
            }else{
                return false;
            }
        }
        return false;
    }

    /**
     * 查询单条记录
     * @param mixed $where 查询条件 array|string
     * @param string $field 获取到的字段
     * @return mixed 返回数据
     */
    public function get_row($where,$field='*'){
        $table = $this->_get_table_name();
        if(!empty($where)) $this->db->where($where, NULL, FALSE);
        if(!empty($field)) $this->db->select($field);
        $query = $this->db->get($table,1);
        return $query->row_array();
    }

    /**
     * 删除内容
     * @param mixed $where 查询条件 array|string
     * @return bool 返回数据
     */
    public function remove_row($where){
        $table = $this->_get_table_name();
        if(!empty($where))return $this->db->delete($table,$where);
        return false;
    }

    /**
     * 根据条件获取游客信息表
     * @param mixed $where 查询条件
     * @param string $field 获取字段
     * @return array $data 返回数据
     */
    public function get_list($where='',$field=''){
        $table = $this->_get_table_name();
        if(!empty($where))$this->db->where($where, NULL, FALSE);
        if(!empty($field))$this->db->select($field);
        $query = $this->db->get($table);
        $data = array();
        foreach($query->result_array() as $row){
            $data[] = $row;
        }
        return $data;
    }

    /**
     * 获取数据表名称
     */
    private function _get_table_name(){
        $table = $this->db->dbprefix("order_tourist");
        return $table;
    }

    public function tour_list($page_size=20,$now_page=1,$where){
        $sid = $GLOBALS['sid'];
        $table = $this->_get_table_name();
        $table_order  = $this->db->dbprefix('order_info');
//        $sql = "select a.* from ".$table." as a left join ".$table_order." as b on b.order_id=a.order_id where a.name<>'' and b.sid=".$sid;
        $sql = "select a.* from ".$table." as a where a.name<>'' and a.sid=".$sid;
        if($where['keywords']){
            $sql.= " and (a.name like '%".$where['keywords']."%' or a.phone_num like '%".$where['keywords']."%' or a.id_number like '%".$where['keywords']."%') ";
        }
        $start = ($now_page-1)*$page_size;
        $sql.= " order by a.id desc limit $start,$page_size  ";
        $query = $this->db->query($sql);
        $list = $query->result_array();
        foreach($list as $key=>$val){
            $list[$key]['card_type'] = '身份证';
            if($val['id_card_type']==2){
                $list[$key]['card_type'] = '护照';
            }
            if($val['id_card_type']==3){
                $list[$key]['card_type'] = '军官证';
            }
            $list[$key]['remove_url'] = site_url('order/client/remove?id='.$val['id']);
            $list[$key]['edit_url'] = site_url('order/client/edit?id='.$val['id']);
        }
        return $list;
    }

    public function tour_count($where){
        $sid = $GLOBALS['sid'];
        $table = $this->_get_table_name();
        $table_order  = $this->db->dbprefix('order_info');
        $sql = "select count(a.id) as num from ".$table." as a  where a.name<>'' and a.sid=$sid";
        if($where['keywords']){
            $sql.= " and (a.name like '%".$where['keywords']."%' or a.phone_num like '%".$where['keywords']."%') ";
        }
        $query = $this->db->query($sql);
        $row = $query->row_array();
        return $row['num'];
    }

    /**
     * 客户年龄段统计
     */
    public function fetch_by_age($start=0,$end=0){
        $table = $this->_get_table_name();
        $year = date('Y');
        $start_y = $year - $end;
        $end_y   = $year - $start;
        $where = " where id_card_type=1 and sid=".$GLOBALS['sid'];
        if($end)$where .= " and birth>=$start_y ";
        if($start)$where .= " and birth<=$end_y ";
        $sql = "select count(*) as num from $table $where ";
        $query = $this->db->query($sql);
        $row = $query->row_array();
        return $row['num'];
    }

}